package com.oa.xinxi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.login.entity.Emp;
import com.oa.xinxi.biz.RulesBiz;
import com.oa.xinxi.entity.JianBao;
import com.oa.xinxi.entity.Rules;
@Controller
@RequestMapping("/rules")
public class RulesController {
	@Resource
	private RulesBiz rulesBiz;
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(required=true,defaultValue="1") Integer page) {
		PageHelper.startPage(page, 10);
		List<Rules> list = rulesBiz.findAll();
		PageInfo<Rules> p=new PageInfo<Rules>(list);
		model.addAttribute("list", list);
		model.addAttribute("page", p);
		return "/xinxi/rules/list";
	}
	@RequestMapping("/preAdd")
	public String add() {
		return "/xinxi/rules/add";
	}
	@RequestMapping("/add")
	public String save(Rules rules,HttpSession session) {
		Emp emp = (Emp)session.getAttribute("emp");
		rules.setCreator(emp.getEmpName());
		rulesBiz.save(rules);
		return "redirect:/rules/list";
	}
	@RequestMapping("/detail/{id}")
	public String detail(Model model,@PathVariable("id") int id) {
		Rules rules = rulesBiz.findById(id);
		model.addAttribute("rules", rules);
		return "/xinxi/rules/detail";
	}
	@RequestMapping("/delete/{id}")
	public String deleteById(Model model,@PathVariable("id") int id) {
		rulesBiz.deleteById(id);
		return "redirect:/rules/list";
	}
	@RequestMapping("/preUpdate/{id}")
	public String updateById(Model model,@PathVariable("id") int id) {
		Rules rules = rulesBiz.findById(id);
		 model.addAttribute("rules", rules);
		return "/xinxi/rules/update";
	}
	@RequestMapping("/update")
	public String DoUpdateById(Rules rules,HttpSession session) {
		Emp emp = (Emp)session.getAttribute("emp");
		rules.setUpdator(emp.getEmpName());
		rulesBiz.update(rules);
		return "redirect:/rules/list";
	}
}
